package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
关联查询
* */
public class JBDCDemo6 {
    public static void main(String[] args) {
        try (
                Connection connection=DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql ="SELECT t.name,t.gender,c.name,c.floor "+
                        "FROM teacher t,class c "+
                        "WHERE t.id=c.teacher_id "+
                        "AND t.name ='王克晶'";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
//                String tName =rs.getString("t.name");
//                String tGender =rs.getString("t.gender");
//                String cName =rs.getString("c.name");
//                int cFloor = rs.getInt("c.floor");
//                System.out.println(tName +","+tGender + ","+cName +","+cFloor);
                //也可以通过字段下标获取对应的值。注:结果集第一个字段下标是1，不是0。

                String tName =rs.getString(1);
                String tGender =rs.getString(2);
                String cName =rs.getString(3);
                int cFloor = rs.getInt(4);
                System.out.println(tName +","+tGender + ","+cName +","+cFloor);


            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
